
/*
 * ContactChooser.java
 *
 * Created on 25 mars 2011, 16:56:42
 */
package com.darrylsite.ihm;

import com.darrylsite.core.MyEntityManager;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

/**
 *
 * @author nabster
 * @web http://www.darrylsite.com
 */
public class ContactChooser extends javax.swing.JDialog
{

    private long[] ids;
    private List<String> contacts = new ArrayList<String>();
    public List<String> selectedContact;

    /** Creates new form ContactChooser */
    public ContactChooser(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
        init();
    }

    private void init()
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                loadGroupList();
            }
        });
    }

    private List<com.darrylsite.entite.Groupe> retrievegroupList()
    {
        EntityManager em = MyEntityManager.emf.createEntityManager();
        Query query = em.createQuery("select g from Groupe g order by g.id");
        return query.getResultList();
    }

    private void loadGroupList()
    {
        List<com.darrylsite.entite.Groupe> liste = retrievegroupList();
        List<String> groupes = new ArrayList<String>();

        ids = new long[liste.size()];
        for (int i = 0; i < liste.size(); i++)
        {
            groupes.add(liste.get(i).getName());
            ids[i] = liste.get(i).getId();
        }
        lstGroupe.setListData(groupes.toArray());
        if (ids.length > 0)
        {
            loadContactByGroup(ids[0]);
            lstGroupe.setSelectedIndex(0);
        }
    }

    private void loadContactByGroup(long grp)
    {
        EntityManager em = MyEntityManager.emf.createEntityManager();
        Query query = em.createQuery("select c from Contact c  where "
                + " c.groupe.id=" + grp + " order by c.nom");
        List<com.darrylsite.entite.Contact> liste = query.getResultList();

        contacts.clear();

        for (int i = 0; i < liste.size(); i++)
        {
            contacts.add(liste.get(i).getNom() + " - " + liste.get(i).getNumero());
        }

        lstContact.setModel(new ListModel()
        {

            @Override
            public void addListDataListener(ListDataListener l)
            {
            }

            @Override
            public Object getElementAt(int index)
            {
                return contacts.get(index);
            }

            @Override
            public int getSize()
            {
                return contacts.size();
            }

            @Override
            public void removeListDataListener(ListDataListener l)
            {
            }
        });

    }

    public String retrievePhoneNumber(String nb)
    {
        return nb.split(" - ")[1];
    }

    public void addContact()
    {
        Object[] numbers = lstContact.getSelectedValues();
        selectedContact = new ArrayList<String>();
        for (Object s : numbers)
        {
            selectedContact.add(retrievePhoneNumber(s.toString()));
        }
    }

    public void addGroupe()
    {
        selectedContact = new ArrayList<String>();
        for (String s : contacts)
        {
            selectedContact.add(retrievePhoneNumber(s));
        }
    }

    public List<String> getSelectedContact()
    {
        return selectedContact;
    }

    public void setSelectedContact(List<String> selectedContact)
    {
        this.selectedContact = selectedContact;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstContact = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstGroupe = new javax.swing.JList();
        btAddContact = new javax.swing.JButton();
        btAddgroupe = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Liste de contacts");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(39, 28, -1, -1));

        jScrollPane1.setViewportView(lstContact);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, 196, 220));

        jLabel2.setText("Groupe disponible");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 30, 122, -1));

        lstGroupe.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstGroupeMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstGroupe);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 60, 177, 220));

        btAddContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        btAddContact.setText("Ajouter");
        btAddContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btAddContactMouseClicked(evt);
            }
        });
        getContentPane().add(btAddContact, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 300, -1, -1));

        btAddgroupe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        btAddgroupe.setText("Ajouter");
        btAddgroupe.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btAddgroupeMouseClicked(evt);
            }
        });
        getContentPane().add(btAddgroupe, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 300, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstGroupeMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lstGroupeMouseClicked
    {//GEN-HEADEREND:event_lstGroupeMouseClicked
        if (lstGroupe.getSelectedIndex() != -1)
        {
            loadContactByGroup(ids[lstGroupe.getSelectedIndex()]);
        }
    }//GEN-LAST:event_lstGroupeMouseClicked

    private void btAddContactMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btAddContactMouseClicked
    {//GEN-HEADEREND:event_btAddContactMouseClicked
        addContact();
        this.setVisible(false);
    }//GEN-LAST:event_btAddContactMouseClicked

    private void btAddgroupeMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btAddgroupeMouseClicked
    {//GEN-HEADEREND:event_btAddgroupeMouseClicked
        addGroupe();
        this.setVisible(false);
    }//GEN-LAST:event_btAddgroupeMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddContact;
    private javax.swing.JButton btAddgroupe;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstContact;
    private javax.swing.JList lstGroupe;
    // End of variables declaration//GEN-END:variables
}
